# -*- coding: utf-8 -*-
import email,sys,time,os,json,re,datetime,smtplib
from email.mime.text import MIMEText
from email.header import Header
from imapclient import IMAPClient
from bs4 import BeautifulSoup


#163测试邮箱
USERNAME = 'hh_octopus@163.com'    #163 邮箱账号
HOSTNAME = 'smtp.163.com'          #163 imap 域名  smtp.163.com
PASSWD   = 'KWIRNRJAEUCTTMIW'     #163 imap 授权码
#LOGOUT_SUCCESS = b'IMAP4rev1 Server logging out'  #163的logout电信的貌似不一样

def PRINT(string):
    "格式化打印字符串函数，在打印的头部增加对应的时间信息"
    print('>>>{},line:{},<<<    '\
    .format(time.strftime( "%b %d %Y %H:%M:%S",time.localtime(time.time()) )\
    ,sys._getframe().f_lineno)\
    ,end="")
    # /print('调用该函数的上级为{}'.format(sys._getframe(1).f_code.co_name))
    print(string)


def main():
    "主函数用于发送下载完成通知邮件到指定的邮箱，指定的邮箱由receivers变量指定"
    COMMASPACE = ', '
    sender = 'hh_octopus@163.com'
    # 接收邮件，可设置为你的QQ邮箱或者其他邮箱
    receivers = ['18275689719@163.com', 'ff_octopus@163.com']  
    
    # 三个参数：第一个为文本内容，第二个 plain 设置文本格式，第三个 utf-8 设置编码
    message = MIMEText('亚信 ftpserver 资源已经下载就绪!', 'plain', 'utf-8')
    #message['From'] = Header("hh_octopus@163.com", 'utf-8')   # 发送者
    message['From'] = 'hh_octopus@163.com'
    # family = the list of all recipients' email addresses
    message['To'] =  COMMASPACE.join(receivers)
    subject = '亚信资源就绪'
    message['Subject'] = Header(subject, 'utf-8')
    
    
    try:
        smtpObj=smtplib.SMTP_SSL("smtp.163.com", 465)        # 465 为 SMTP 安全端口号
        ret2 = smtpObj.login(USERNAME,PASSWD)  
        PRINT(ret2)
        ret3 = smtpObj.sendmail(sender, receivers, message.as_string())
        PRINT(ret3)
        PRINT("[infor] send mail success!")
    except smtplib.SMTPException:
        PRINT("[error] send mail exception!")

#****************************************
#*****主程序 
#**************************************** 
if __name__ == '__main__':
    main()